﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;

namespace MyUtility.EasyHtmlHelper {
    public static class EasyHtmlHelperSelect {
        #region Select下拉框
        /// <summary>
        /// EasyUI combobox格式 可以IEnumerable<SelectListItem>
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="selectList"></param>
        /// <param name="width"></param>
        /// <param name="editable">是否可编辑 默认为false</param>
        /// <param name="required">是否必填 默认为false</param>
        /// <returns></returns>
        public static IHtmlContent EasyDropDownListFor<TModel, TResult>(this IHtmlHelper<TModel> htmlHelper,
            Expression<Func<TModel, TResult>> expression,
            IEnumerable<SelectListItem> selectList = null, string width = "80%", bool editable = false, bool required = false) {
            if (htmlHelper == null) {
                throw new ArgumentNullException(nameof(htmlHelper));
            }

            if (expression == null) {
                throw new ArgumentNullException(nameof(expression));
            }
            string editStr = editable == true ? "true" : "false";
            string requiredStr = required == true ? "true" : "false";
            string editableStr = $"editable:{editStr},required:{requiredStr}";
            string widthStr = $"width:{width}";
            if (selectList == null) { selectList = new List<SelectListItem>(); }
            return htmlHelper.DropDownListFor(expression, selectList, new { @class = "easyui-combobox", data_options = editableStr, @style = widthStr });
        }

        /// <summary>
        /// EasyUI combobox格式 可以数组形式传入
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="arrList">数组形式传入</param>
        /// <param name="width"></param>
        /// <param name="editable">是否可编辑 默认为false</param>
        /// <param name="required">是否必填 默认为false</param>
        /// <returns></returns>
        public static IHtmlContent EasyDropDownListFor<TModel, TResult>(this IHtmlHelper<TModel> htmlHelper,
            Expression<Func<TModel, TResult>> expression,
            string[] arrList, string width = "80%", bool editable = false, bool required = false) {
            if (htmlHelper == null) {
                throw new ArgumentNullException(nameof(htmlHelper));
            }

            if (expression == null) {
                throw new ArgumentNullException(nameof(expression));
            }

            //IEnumerable<SelectListItem> selectList;
            var selectList = arrList.Select(b => new SelectListItem { Text = b, Value = b });
            string editStr = editable == true ? "true" : "false";
            string requiredStr = required == true ? "true" : "false";
            string editableStr = $"editable:{editStr},required:{requiredStr}";
            string widthStr = $"width:{width}";
            return htmlHelper.DropDownListFor(expression, selectList, new { @class = "easyui-combobox", data_options = editableStr, @style = widthStr });
        }
        #endregion

        #region Select下拉框不带expression表达式
        /// <summary>
        /// EasyUI combobox格式 可以IEnumerable<SelectListItem>
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="selectList"></param>
        /// <param name="width"></param>
        /// <param name="editable">是否可编辑 默认为false</param>
        /// <param name="required">是否必填 默认为false</param>
        /// <returns></returns>
        public static IHtmlContent EasyDropDownList(this IHtmlHelper htmlHelper,
            string expression,
            IEnumerable<SelectListItem> selectList = null, string width = "80%", bool editable = false, bool required = false) {
            if (htmlHelper == null) {
                throw new ArgumentNullException(nameof(htmlHelper));
            }

            string editStr = editable == true ? "true" : "false";
            string requiredStr = required == true ? "true" : "false";
            string editableStr = $"editable:{editStr},required:{requiredStr}";
            string widthStr = $"width:{width}";
            if (selectList == null) { selectList = new List<SelectListItem>(); }
            return htmlHelper.DropDownList(expression, selectList, new { @class = "easyui-combobox", data_options = editableStr, @style = widthStr });
        }

        /// <summary>
        /// EasyUI combobox格式 可以数组形式传入
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="arrList">数组形式传入</param>
        /// <param name="width"></param>
        /// <param name="editable">是否可编辑 默认为false</param>
        /// <param name="required">是否必填 默认为false</param>
        /// <returns></returns>
        public static IHtmlContent EasyDropDownList(this IHtmlHelper htmlHelper,
            string expression,
            string[] arrList, string width = "80%", bool editable = false, bool required = false) {
            if (htmlHelper == null) {
                throw new ArgumentNullException(nameof(htmlHelper));
            }

            var selectList = arrList.Select(b => new SelectListItem { Text = b, Value = b });
            string editStr = editable == true ? "true" : "false";
            string requiredStr = required == true ? "true" : "false";
            string editableStr = $"editable:{editStr},required:{requiredStr}";
            string widthStr = $"width:{width}";
            return htmlHelper.DropDownList(expression, selectList, new { @class = "easyui-combobox", data_options = editableStr, @style = widthStr });
        }


        #endregion
    }
}
